// pages/user/index.js
import {
  request
} from '../../api/index'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showPlus: true,
    modifyPasswordObj: {
      show: false,
      tempPwd: '',
      name: '',
      id: ''
    },
    modifyNameObj: {
      show: false,
      tempName: '',
      id: ''
    },
    refresh: false,
    userAddShow: false,
    page: {
      name: '',
      currentPage: 1,
      size: 7,
      isAsc: false
    },
    total: 50,
    dataSource: []
  },
  setPage() {
    wx.setNavigationBarTitle({
      title: '用户管理'
    })
  },
  onSearch(event) {
    const name = event.detail;
    this.setData({
      page: {
        ...this.data.page,
        currentPage: 1,
        name
      }
    })
    this.fetchData();
  },
  onFocus() {
    this.setData({
      showPlus: false
    })
  },
  onBlur() {
    this.setData({
      showPlus: true
    })
  },
  onRefresh() {
    this.setData({
      refresh: true,
      page: {
        ...this.data.page,
        currentPage: 1
      }
    })
    this.fetchData();
    setTimeout(() => {
      this.setData({
        refresh: false
      })
    }, 1200)
  },
  onScrollLoad() {
    if (this.data.total > this.data.dataSource.length) {
      this.setData({
        onLoading: true,
        page: {
          ...this.data.page,
          currentPage: this.data.page.currentPage + 1
        }
      })
      this.loadData();
      setTimeout(() => {
        this.setData({
          onLoading: false
        })
      }, 1200)
    }
  },
  loadData() {
    request.userSearch(this.data.page, (e) => {
      this.setData({
        dataSource: [...this.data.dataSource, ...e.records],
        total: e.total,
        page: {
          ...this.data.page,
          currentPage: e.current
        }
      })
    }, function (e) {
      Toast({
        message: e,
        position: 'bottom'
      });
    })
  },
  fetchData() {
    if (!this.data.refresh) {
      wx.showLoading({
        title: '加载中'
      });
      setTimeout(() => {
        wx.hideLoading();
      }, 600);
    }
    request.userSearch(this.data.page, res => {
      this.setData({
        dataSource: res.records,
        total: res.total,
        page: {
          ...this.data.page,
          currentPage: res.current
        }
      })
    })
  },
  onRootClick(event) {
    const isRoot = event.currentTarget.dataset.root;
    Toast({
      duration: 1000,
      position: 'bottom',
      message: isRoot ? '管理员' : '普通用户'
    });
  },
  onNameClose() {
    this.setData({
      modifyNameObj: {
        tempName: '',
        id: '',
        show: false
      }
    })
  },
  onNameOpen(event) {
    const {
      item
    } = event.currentTarget.dataset;
    this.setData({
      modifyNameObj: {
        ...this.data.modifyNameObj,
        id: item.id,
        tempName: item.name,
        show: true
      }
    })
  },
  onNameChange(event) {
    const value = event.detail;
    this.setData({
      modifyNameObj: {
        ...this.data.modifyNameObj,
        tempName: value
      }
    })
  },
  modifyName() {
    request.userUpdate({
      name: this.data.modifyNameObj.tempName,
      id: this.data.modifyNameObj.id,
    }, this.localChange.bind(this, this.data.modifyNameObj.id, 'name', this.data.modifyNameObj.tempName))
  },
  onPasswordOpen(event) {
    this.setData({
      modifyPasswordObj: {
        ...this.data.modifyPasswordObj,
        show: true,
        name: event.currentTarget.dataset.item.name,
        id: event.currentTarget.dataset.item.id,
        tempPwd: ''
      }
    })
  },
  onPasswordClose() {
    this.setData({
      modifyPasswordObj: {
        ...this.data.modifyPasswordObj,
        show: false,
        tempPwd: '',
        id: ''
      }
    })
  },
  onPasswordChange(event) {
    this.setData({
      modifyPasswordObj: {
        ...this.data.modifyPasswordObj,
        tempPwd: event.detail
      }
    })
  },
  modifyPassword() {
    const {
      tempPwd,
      id
    } = this.data.modifyPasswordObj;
    if (!tempPwd || tempPwd?.length < 1) {
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '新密码无效'
      })
      return
    }
    request.rootPasswordUpdate({
      password: tempPwd,
      userId: id
    }, this.localChange.bind(this, id, 'password', tempPwd));
    this.onPasswordClose();
  },

  onChange(event) {
    const value = event.detail;
    const {
      id
    } = event.currentTarget.dataset.item;
    request.rootEnabledUpdate({
      enabled: value,
      id
    }, this.localChange.bind(this, id, 'enabled', value))
  },
  localChange(id, key, value) {
    const dataSource = [...this.data.dataSource];
    const index = dataSource.findIndex(it => {
      return it.id === id
    })
    if (index > -1) {
      dataSource[index][key] = value;
    }
    this.setData({
      dataSource
    })
  },
  onUserAddClose(event) {
    const refresh = event.detail === true;
    refresh && this.onReset();
    this.setData({
      userAddShow: false
    })
  },
  onUserAddShow() {
    this.setData({
      userAddShow: true
    })
  },
  onClear() {
    this.setData({
      page: {
        ...this.data.page,
        name: ''
      }
    })
    this.onReset();
  },
  onReset() {
    this.setData({
      page: {
        ...this.data.page,
        currentPage: 1
      }
    })
    this.fetchData();
  },
  onDelete(event) {
    const {
      item
    } = event.currentTarget.dataset;
    wx.showModal({
      content: '确认要删除[' + item.name + ']吗？',
      success: res => {
        if (res.confirm) {
          request.userRemove({
            id: item.id
          }, () => {
            const dataSource = [...this.data.dataSource];
            const index = dataSource.findIndex(it => it.id === item.id);
            dataSource.splice(index, 1);
            this.setData({
              dataSource
            })
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setPage();
    this.fetchData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})